﻿#include "ssColorRender.h"
#include "../material/ssMaterial.h"
#include "../../resmgr/ssResManager.h"


namespace StarSeeker
{

ssColorRender::ssColorRender()
{
	m_color = 0;
}

ssColorRender::~ssColorRender()
{
	if (!m_pMesh)
	{
		SS_SAFE_DELETE(m_color)
	}
}

bool ssColorRender::SetColor(u32 num, f32* pc)
{
	if (!(m_VDF&SS_VDF_COLOR_4F))
	{
		SS_SAFE_NEW(m_color, ssVBOBind)
		if (m_color->CreateFloat(pc, num))
		{
			m_VDF |= SS_VDF_COLOR_4F;
			m_colorAttrib.BindData(4, SS_FLOAT, 0, 0, "inColor");
			return true;
		}
		SS_SAFE_DELETE(m_color)
	}
	return false;
}

void ssColorRender::SetDataFromMesh(const char* uri)
{
	ssMeshRender::SetDataFromMesh(uri);
	if (m_pMesh)
	{
		m_color = m_pMesh->GetColorVBO();
		if (m_color)
		{
			m_VDF |= SS_VDF_COLOR_4F;
			m_colorAttrib.BindData(4, SS_FLOAT, 0, 0, "inColor");
		}
	}
}

void ssColorRender::OnRenderBegin()
{
	ssMeshRender::OnRenderBegin();
	if (m_VDF&SS_VDF_COLOR_4F)
	{
		m_color->Use();
		m_colorAttrib.Use(ssMaterial::s_pShader);
	}
}

void ssColorRender::OnRenderEnd()
{
	ssMeshRender::OnRenderEnd();
	if (m_VDF&SS_VDF_COLOR_4F)
	{
		m_color->Nonuse();
		m_colorAttrib.Nonuse();
	}
}

}
